//
//  LDMainTabBar.swift
//  LDMicroblog
//
//  Created by pro on 15/8/29.
//  Copyright (c) 2015年 pro. All rights reserved.
//

import UIKit

class LDMainTabBar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        
//        println("\(__FUNCTION__)")
        //MARK:--重新布局
//        println(self.subviews)
        let count = 5
        let w = self.bounds.size.width / CGFloat(count)
        let h = self.bounds.size.height
        
        var frame = CGRectMake(0, 0, w, h)
        
        var index = 0
        for v in subviews as! [UIView] {
       
            if v is UIControl && !(v is UIButton) {
           
//                println(v)
                //偏移方法
                v.frame = CGRectOffset(frame, w * CGFloat(index), 0)
                
                //空出中间的位置
                index++
                
                if index == 2 {
               
                    index++
                }
//                index += index == 1 ? 2 : 1 //易读性不高
                
            }
        }
        self.plusBtn.frame = CGRectOffset(frame, 2 * w, 0)
//        self.plusBtn.bounds = CGRectMake(0, 0, w, h)
//        self.plusBtn.center = CGPointMake(self.frame.size.width * 0.5, h * 0.5)
        
    }
    
    lazy var plusBtn: UIButton = {//加号按钮
        
//        let plusBtn = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        let plusBtn = UIButton()
        
        plusBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        plusBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        plusBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        plusBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(plusBtn)//因为懒加载的闭包需在调用时才执行，所以需要使用self.
        
        
        return plusBtn
    
        
    }()

}
